const BaseHandler = require('../base-handler');
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;

class AccountsHandler extends BaseHandler {

    get pattern() {
        return '/me/accounts';
    }

    doGET(request, response) {
        if(!request.session.user.authenticated) {
            return response.json({
                success: false,
                error: '请登录系统后操作！'
            });
        }

        let userId = request.session.user.id;
        let socialType = request.query.social_type;

        this.Controller.getManagedOfficialAccountsByUserIdAndType(userId, socialType, (result) => {

            if (result.success && socialType && result.data.accounts.length === 0) {
                let payload = 'unknown';
                switch(socialType) {
                case 'wechat':
                    payload = `http://${request.headers['host']}/wechat/platform/redirect`;
                break;
                case 'dianping':
                    payload = 'dianping';
                break;
                case 'weibo':
                    payload = `http://${request.headers.host}/weibo/platform/redirect`;
                break;
                }

                result.data.payload = payload;
            }

            response.json(result);
        });
    }

    doPOST(request, response) {
        if(!request.session.user.authenticated) {
            response.json({
                success: false,
                error: '请重新登录！'
            });
        } else {
            let data = request.body;
            let userId = request.session.user.id;
            let socialType = data.socialType || (data.appId && data.appSecret && 'wechat');
            var account = {
                user_id: userId,
                social_type: socialType
            };
            if (socialType === 'wechat') {
                account.app_id = data.appId;
                account.access_token = data.appSecret;
                account.refresh_token = null;
                account.type = 'normal';
            } else if (socialType === 'dianping') {
                if (!data.shopId) {
                    return response.json({
                        success: false,
                        error : "请输入点评商家编号！"
                    });
                }

                if (!data.shopName) {
                    return response.json({
                        success: false,
                        error: "请输入点评商家名称！"
                    });
                }

                account.app_id = data.shopId;
                account.name = data.shopName;
            } else {
                return response.json({
                    success: false,
                    error: '不支持的类型'
                });
            }
            
            this.Controller.saveOrUpdateAccount(userId, account, (error, data) => {
                if(error) {
                    response.end(JSON.stringify({
                        success: false,
                        error: error
                    }))
                } else {
                    response.end(JSON.stringify({
                        success: true,
                        data: data
                    }));
                }
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = AccountsHandler;